﻿using System.Collections.Generic;
using XLua;
using UnityEngine;
using WorldMapBase;
using WorldMapBase.Collision;

/// <summary>
/// 动态层管理器
/// </summary>
public static class DynamicLayerMgr
{
    public static WorldEntry worldEntry;
    public static WorldMapFog worldMapFog;
    public static MapWonderArea wonderArea;
    /// <summary>
    /// 格子宽度
    /// </summary>
    private static int gridWidth = 4;

    /// <summary>
    /// 格子高度
    /// </summary>
    private static int gridHeight = 2;

    /// <summary>
    /// 主城层
    /// </summary>
    // private static CityLayer cityLayer;

    /// <summary>
    /// 创建地图层
    /// </summary>
    public static void InitLayers(WorldEntry value)
    {
        worldEntry = value;
    }

    /// <summary>
    /// 创建迷雾层
    /// </summary>
    public static void InitFogLayers(WorldMapFog value)
    {
        worldMapFog = value;
    }

    /// <summary>
    /// 创建势力层
    /// </summary>
    public static void InitWonderAreaLayers(MapWonderArea value)
    {
        wonderArea = value;
    }

    public static void InitResPool(LuaTable value)
    {
        var iList = new List<string>();
        for (int i = 1; i <= value.Length; i++)
        {
            // Debug.Log(value[i]);
            iList.Add(value[i].ToString());
        }
        
//         resPointLayer.Init(iList,() =>
//         {
//             //初始化完成才能添加
// #if UNITY_EDITOR
//             AddInitLayerData();
// #endif
//             Debug.Log("InitResPool ok");
//         });
        
    }

    /// <summary>
    /// 设置层级每个格子的尺寸
    /// </summary>
    /// <param name="width">格子宽度（米）</param>
    /// <param name="height">格子高度（米）</param>
    public static void SetLayerGridSize(int width, int height)
    {
        gridWidth = width;
        gridHeight = height;
        worldEntry.SetCameraGridSize(width, height);
    }

    /// <summary>
    /// 添加摄像机的LOD格子范围
    /// </summary>
    /// <param name="lodLevel">LOD等级</param>
    /// <param name="gridRow">多少列格子</param>
    /// <param name="gridCol">多少行格子</param>
    public static void AddCameraLODGridRange(int lodLevel, int gridRow, int gridCol)
    {
        worldEntry.AddCameraLODGridRange(lodLevel, gridRow, gridCol);
    }

    /// <summary>
    /// 创建沙盘初始化数据
    /// </summary>
    public static void AddInitLayerData()
    {
        //marchLayer.AddObject(new MarchData();
        // cityLayer.AddObject(new CityData(new Vector2(280, 270)) { id = 1 });
    }

    #region 添加动态层级

    /// <summary>
    /// 添加LOD层级容器
    /// </summary>
    /// <param name="table"></param>
    /// <returns></returns>
    public static LuaLODLayerContainer AddLODLayerContainer(LuaTable table)
    {
        LuaLODLayerContainer container = new LuaLODLayerContainer(table);
        worldEntry.AddCustomLODContainer(container);
        return container;
    }

    /// <summary>
    /// 移除LOD层级容器
    /// </summary>
    /// <param name="container"></param>
    public static void RemoveLODLayerContainer(LuaLODLayerContainer container)
    {
        worldEntry.RemoveCustomLODContainer(container);
    }
    # endregion

    /// <summary>
    /// 通过LUA测添加带有LOD功能的层级
    /// </summary>
    /// <param name="table"></param>
    /// <returns></returns>
    public static LuaLODLayer AddLuaLODLayer(LuaTable table)
    {
        var layer = new LuaLODLayer(table);
        worldEntry.AddCustomLODLayer(layer);
        return layer;
    }
    
    /// <summary>
    /// 添加一个Lua端在运行时动态添加的离散分布对象层级
    /// </summary>
    /// <param name="table">Lua端对应的表格</param>
    /// <returns>层级对象</returns>
    public static LuaDiscreteObjectLODLayer AddLuaDiscreteObjectLODLayer(LuaTable table)
    {
        var layer = New_LuaDiscreteObjectLODLayer(table);
        worldEntry.AddCustomLODLayer(layer);
        return layer;
    }

    /// <summary>
    /// 新建一个Lua端在运行时动态添加的离散分布对象层级
    /// </summary>
    /// <param name="table">Lua端对应的表格</param>
    /// <returns>层级对象</returns>
    public static LuaDiscreteObjectLODLayer New_LuaDiscreteObjectLODLayer(LuaTable table)
    {
        var layer = new LuaDiscreteObjectLODLayer(table, gridWidth, gridHeight);
        return layer;
    }

    /// <summary>
    /// 新建沙盘碰撞显示的层级
    /// </summary>
    /// <returns></returns>
    public static WorldMapBaseCollisionLayer New_WorldMapBaseCollisionLayer()
    {
        return new WorldMapBaseCollisionLayer(10, new Unity.Mathematics.int4());
    }

    /// <summary>
    /// 移除树层级的碰撞区域（用于重新显示树）
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    /// <param name="r"></param>
    public static void AddTreeLayerCollision(float x, float y, float r)
    {
        //worldEntry.AddTreeLayerCollision(x, y, r);
    }

    /// <summary>
    /// 设置树的碰撞数据以及树层级检测碰撞的位数据
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    /// <param name="r"></param>
    public static void RemoveTreeLayerCollision(float x, float y, float r)
    {
        //worldEntry.RemoveTreeLayerCollision(x, y, r);
    }

    /// <summary>
    /// 创建城堡用的碰撞
    /// </summary>
    /// <param name="gridSize"></param>
    /// <returns></returns>
    public static CircleSetsCollisionData CreateCityCollision(int gridSize)
    {
        return new CircleSetsCollisionData(gridSize);
    }

    /// <summary>
    /// 创建沙盘的碰撞
    /// </summary>
    /// <returns></returns>
    public static NavimeshCollisionData CreateWorldMapBaseCollision()
    {
        return new NavimeshCollisionData();
    }

    /// <summary>
    /// 设置沙盘的出生点 占领检查 -ture 只显示已被占领的联盟领地的出生点
    /// </summary>
    /// <returns></returns>
    public static void SetMapPointCheck(bool enabled)
    {
        if (worldEntry != null)
        {
            worldEntry.SetMapPointCheck(enabled);
        }
    }

    public static void SetAllianceChunck(int tileIndex, bool value)
    {
        if (worldEntry != null)
        {
            worldEntry.SetAllianceChunck(tileIndex, value);
        }
    }

    /// <summary>
    /// 显示沙盘的出生点
    /// </summary>
    /// <returns></returns>
    public static void ShowMapPoint(bool enabled)
    {
        if (worldEntry != null)
        {
            worldEntry.ShowMapPoint(enabled);
        }
    }

    /// <summary>
    /// 显示沙盘的联盟建造点
    /// </summary>
    /// <returns></returns>
    public static void ShowAllianceBuildPoint(bool enabled)
    {
        if (worldEntry != null)
        {
            worldEntry.ShowAllianceBuildPoint(enabled);
        }
    }

    /// <summary>
    /// 检测沙盘的地形碰撞
    /// </summary>
    /// <returns></returns>
    public static bool ShowTerrainCollider(Vector3 pos, float r, int layerMask)
    {
        bool res = true;

        //if (worldEntry != null)
        //{
        //    res = worldEntry.CheckTerrainCollider(pos,r,layerMask);
        //}
        return res;
    }

    /// <summary>
    /// 当前是否在内城lod限制
    /// </summary>
    public static bool inCityLod = true;

    /// <summary>
    /// 设置沙盘缩放参数
    /// </summary>
    /// <param name="inCity">是否在内城视角（bool）</param>
    /// <param name="viewCenter">显示视野的中心点（Vector2）</param>
    public static void SetCityLod(bool inCity,Vector2 viewCenter)
    {
        // 设置lod限制参数
        inCityLod = inCity;
    }

    public static string CheckClickInOverSea(float x,float y)
    {
        return WorldMapConfigData.CheckPointInOverSea(x, y);
    }
}